package com.perfree.task;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.perfree.common.DateUtil;
import com.perfree.common.GoFastDfsApi;
import com.perfree.controller.BaseController;
import com.perfree.entity.Peers;
import com.perfree.mapper.PeersMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Component
@Slf4j
public class ClearTask  extends BaseController {

    @Autowired
    private PeersMapper peersMapper;

    @Scheduled(fixedDelay = 1000*10*60)
    @Async
    public void clearEmptyDir() {
        List<Peers> peers = peersMapper.getAllPeers();
       for (Peers peer :peers){
           String result = HttpUtil.post(peer.getServerAddress() + GoFastDfsApi.REMOVE_EMPTY_DIR, new HashMap<>());
           JSONObject parseObj = JSONUtil.parseObj(result);
           if(parseObj.get("status").equals("ok")) {
               log.info(DateUtil.getFormatDate(new Date(),null)+":操作成功,正在后台操作");
           }else{
               log.error(DateUtil.getFormatDate(new Date(),null)+"操作失败,请稍后再试");
           }
       }
    }
}
